/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2020-08-17
 * Time: 23:42
 */
#include "DragTreeWidget.h"

DragTreeWidget::DragTreeWidget(QWidget *parent)
        : QWidget(parent), left_tree_widget_(nullptr), right_tree_widget_(nullptr), left_top_item1_(
        new QTreeWidgetItem(QStringList() << "A")), left_top_item2_(new QTreeWidgetItem(QStringList() << "B"))
          , right_top_item1_(new QTreeWidgetItem(QStringList() << "C")), right_top_item2_(
                new QTreeWidgetItem(QStringList() << "D"))
{
    InitUi();
    InitEvent();
}

DragTreeWidget::~DragTreeWidget()
{

}

void DragTreeWidget::InitUi()
{
    QHBoxLayout *qhBoxLayout = new QHBoxLayout(this);

    left_tree_widget_ = new QTreeWidget();
    left_tree_widget_->setSelectionMode(QAbstractItemView::ExtendedSelection);      //设置选择模式为多选模式
    left_tree_widget_->setDragEnabled(true);
    left_tree_widget_->setAcceptDrops(true);
    left_tree_widget_->setDefaultDropAction(Qt::MoveAction);
    //left_tree_widget_->setDragDropMode(QAbstractItemView::DragDrop);
    //left_tree_widget_->setDragDropMode(QAbstractItemView::InternalMove);


    right_tree_widget_ = new QTreeWidget();
    right_tree_widget_->setSelectionMode(QAbstractItemView::ExtendedSelection);
    right_tree_widget_->setDragEnabled(true);
    right_tree_widget_->setAcceptDrops(true);
    right_tree_widget_->setDefaultDropAction(Qt::MoveAction);           //设置拖放模式，复制、移动
    right_tree_widget_->setDragDropMode(QAbstractItemView::InternalMove);   //开启内部复制

    qhBoxLayout->addWidget(left_tree_widget_);
    qhBoxLayout->addWidget(right_tree_widget_);


    left_tree_widget_->setHeaderLabels(QStringList() << "left");
    left_tree_widget_->addTopLevelItem(left_top_item1_);
    left_tree_widget_->addTopLevelItem(left_top_item2_);

    QList<QTreeWidgetItem *> left_list1;
    left_list1 << new QTreeWidgetItem(QStringList() << "a1")
               << new QTreeWidgetItem(QStringList() << "a2")
               << new QTreeWidgetItem(QStringList() << "a3");
    left_top_item1_->addChildren(left_list1);

    QList<QTreeWidgetItem *> left_list2;
    left_list2 << new QTreeWidgetItem(QStringList() << "b1")
               << new QTreeWidgetItem(QStringList() << "b2")
               << new QTreeWidgetItem(QStringList() << "b3");
    left_top_item2_->addChildren(left_list2);

    left_tree_widget_->expandAll();

    right_tree_widget_->setHeaderLabels(QStringList() << "right");
    right_tree_widget_->addTopLevelItem(right_top_item1_);
    right_tree_widget_->addTopLevelItem(right_top_item2_);

    QList<QTreeWidgetItem *> right_list1;
    right_list1 << new QTreeWidgetItem(QStringList() << "c1")
                << new QTreeWidgetItem(QStringList() << "c2")
                << new QTreeWidgetItem(QStringList() << "c3");
    right_top_item1_->addChildren(right_list1);

    QList<QTreeWidgetItem *> right_list2;
    right_list2 << new QTreeWidgetItem(QStringList() << "d1")
                << new QTreeWidgetItem(QStringList() << "d2")
                << new QTreeWidgetItem(QStringList() << "d3");
    right_top_item2_->addChildren(right_list2);

    right_tree_widget_->expandAll();
}

void DragTreeWidget::InitEvent()
{
    connect(right_tree_widget_,&QTreeWidget::itemClicked,[=](QTreeWidgetItem *item, int column){
        qDebug()<<&item;
    });
}
